package com.vt.common.core.util.business;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.vt.common.core.util.business.enums.FlowNoEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;

/**
 * @description : 有序编号和无序字符生成工具
 */
@Slf4j
@Component
public class NumberUtil {
    private static String FS = "yyyyMMddHHmmss";

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 根据前缀\长度生成编号
     *
     * @param flowNoEnum
     * @return
     */
    public String genNO(FlowNoEnum flowNoEnum) {
        String timeStamp = DateUtil.format(new Date(), FS);
        int flowNo = increment(flowNoEnum.key + timeStamp).intValue();
        return flowNoEnum.subfix + timeStamp + formatNumToLength(flowNo, flowNoEnum.length);
    }


    /**
     * 生成序列号
     *
     * @param key
     * @return
     */
    public Long increment(final String key) {
        try {
            ValueOperations<String, String> operations = redisTemplate.opsForValue();
            if (!redisTemplate.hasKey(key)) {
                //如果key不存在,表示原有的key已经超时,需要删除
                String prefix = key.substring(0, key.lastIndexOf("_")) + "*";
                Set<String> keys = redisTemplate.keys(prefix);
                redisTemplate.delete(keys);

            }
            return operations.increment(key, 1);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 根据前缀\长度生成编号
     *
     * @param key
     * @param prefix
     * @param length
     * @return
     */
    public String genNO(String key, String prefix, int length) {
        int flowNo = increment(key).intValue();
        return prefix + formatNumToLength(flowNo, length);
    }

    /**
     * 根据前缀\长度生成编号
     *
     * @param key
     * @param prefix
     * @param timeStamp
     * @param length
     * @return
     */
    public String genNO(String key, String prefix, String timeStamp, int length) {
        int flowNo = increment(key).intValue();
        timeStamp = StrUtil.isNotEmpty(timeStamp) ? timeStamp : "";
        return prefix + timeStamp + formatNumToLength(flowNo, length);
    }


    /**
     * 格式化数字,填充至指定长度
     *
     * @param num 数字
     * @param len 长度
     * @param
     * @return
     */
    public String formatNumToLength(int num, int len) {
        return formatNumToLength(num, len, "0");
    }

    /**
     * 格式化数字,填充至指定长度
     *
     * @param num 数字
     * @param len 长度
     * @param sep 填充字符,默认0
     * @return
     */
    public String formatNumToLength(int num, int len, String sep) {
        if (null == sep) sep = "0";
        StringBuffer sb = new StringBuffer();
        String numStr = num + "";
        for (int i = numStr.length(); i < len; i++) {
            sb.append(sep);
        }
        sb.append(numStr);
        return sb.toString();
    }
}
